package com.api.shopapi.mapper;


import com.api.shopapi.entity.Zone;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface ZoneMapper {

    @Insert("insert into zone" +
            "(" +
            "ZoneCode," +
            "ParentZoneCode," +
            "Name," +
            "Sort," +
            "Level," +
            "GovernmentCode" +
            ")values(" +
            "#{zone.ZoneCode}," +
            "#{zone.ParentZoneCode}," +
            "#{zone.Name}," +
            "#{zone.Sort}," +
            "#{zone.Level}," +
            "#{zone.GovernmentCode}" +
            ")")
    int insert(@Param("zone") Zone zone);


    @Delete("delete from zone where zoneCode=#{zoneCode}")
    int deleteByZoneCode(@Param("zoneCode") String zoneCode);


    @Update("update zone set " +
            "ParentZoneCode=#{zone.ParentZoneCode}," +
            "Name=#{zone.Name}," +
            "Sort=#{zone.Sort}," +
            "Level=#{zone.Level}," +
            "GovernmentCode=#{zone.GovernmentCode} " +
            " where ZoneCode=#{zone.ZoneCode}")
    int updateByZoneCode(@Param("zone") Zone zone);


    @Select("select * from zone where zoneCode=#{zoneCode} limit 1")
    Zone getByZoneCode(@Param("zoneCode") String zoneCode);


    @Select("select * from zone order by Sort desc")
    List<Zone> getList();

    @Select("select * from zone where parentZoneCode=#{parentZoneCode} order by Sort desc")
    List<Zone> getListByParentZoneCode(String parentZoneCode);

    @Select("select * from zone where parentZoneCode=#{parentZoneCode} and governmentCode=#{governmentCode} order by Sort desc")
    List<Zone> getListByParentZoneCodeGovernmentCode(@Param("parentZoneCode")String parentZoneCode,@Param("governmentCode") String governmentCode);

//通过zoneCode去查找
    @Select("select * from zone where ZoneCode=#{ZoneCode} and governmentCode=#{governmentCode} order by Sort desc")
    List<Zone> getListByParentZoneCodeGovernmentCode1(@Param("ZoneCode")String zoneCode,@Param("governmentCode") String governmentCode);


    @Select("select * from zone where ParentZoneCode=#{parentZoneCode} limit 1 ")
    Zone getByByParentZoneCode(@Param("parentZoneCode") String parentZoneCode);


//    @Select("select  * from " + "zone " + "where " + "Name=#{name} ")
//    Zone getByName(@Param("name") String name);




    @Select("select * from  zone " +
            "where " +
            "Name=#{name} " +
            "and " +
            "GovernmentCode=#{governmentCode} " +
            "limit 1")
    Zone getByName(@Param("name") String name,@Param("governmentCode") String governmentCode);

    @Select("select * from zone where GovernmentCode=#{governmentCode}")
    List<Zone> getListByGovernmentCode(@Param("governmentCode")String governmentCode);

    @Select("select * from zone where ParentZoneCode=#{parentZoneCode} and GovernmentCode=#{governmentCode} ")
    Zone getZoneList(@Param("parentZoneCode") String parentZoneCode, @Param("governmentCode") String governmentCode);
}
